/*
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*
*/
/*
File:       TCPSocket.cpp

Contains:   implements TCPSocket class



*/

#ifndef __Win32__
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif

#include <errno.h>
#include <assert.h>

#include "TCPSocket.h"
#include "OSThread.h"

#ifdef USE_NETLOG
#include <netlog.h>
#endif

void TCPSocket::SnarfSocket(TCPSocket & fromSocket)
{
    // take the connection away from the other socket
    // and use it as our own.
    assert(m_s32FileDesc == kInvalidFileDesc);
    this->Set(fromSocket.m_s32FileDesc, &fromSocket.fRemoteAddr);

    // clear the old socket so he doesn't close and the like
    struct  sockaddr_in  remoteaddr;

    ::memset(&remoteaddr, 0, sizeof(remoteaddr));

    fromSocket.Set(Socket::kInvalidFileDesc, &remoteaddr);

    // get the event context too
    //this->SnarfEventContext(fromSocket);

}

void TCPSocket::Set(int inSocket, struct sockaddr_in* remoteaddr)
{
    fRemoteAddr = *remoteaddr;
    m_s32FileDesc = inSocket;

    if (inSocket != Socket::kInvalidFileDesc)
    {
        //make sure to find out what IP address this connection is actually occuring on. That
        //way, we can report correct information to clients asking what the connection's IP is
#if __Win32__ || __osf__ || __sgi__ || __hpux__	
        int len = sizeof(fLocalAddr);
#else
        socklen_t len = sizeof(fLocalAddr);
#endif
        int err = ::getsockname(m_s32FileDesc, (struct sockaddr*)&fLocalAddr, &len);
        //AssertV(err == 0, OSThread::GetErrno());
        m_u32State |= kBound;
        m_u32State |= kConnected;
    }
    else
        m_u32State = 0;
}

char*  TCPSocket::GetRemoteAddrStr()
{
    char* addr = inet_ntoa(fRemoteAddr.sin_addr);
    strcpy(fRemoteBuffer, addr);
    return fRemoteBuffer;
}

OS_Error  TCPSocket::Connect(UInt32 inRemoteAddr, UInt16 inRemotePort)
{
    ::memset(&fRemoteAddr, 0, sizeof(fRemoteAddr));
    fRemoteAddr.sin_family = AF_INET;        /* host byte order */
    fRemoteAddr.sin_port = htons(inRemotePort); /* short, network byte order */
    fRemoteAddr.sin_addr.s_addr = htonl(inRemoteAddr);

    /* don't forget to error check the connect()! */
    int err = ::connect(m_s32FileDesc, (sockaddr *)&fRemoteAddr, sizeof(fRemoteAddr));
    m_u32State |= kConnected;

    if (err == -1)
    {
        fRemoteAddr.sin_port = 0;
        fRemoteAddr.sin_addr.s_addr = 0;
        return (OS_Error)OSThread::GetErrno();
    }

    return OS_NoErr;

}

OS_Error TCPSocket::Connect(const char * inRemoteAddr, UInt16 inRemotePort)
{
    ::memset(&fRemoteAddr, 0, sizeof(fRemoteAddr));
    fRemoteAddr.sin_family = AF_INET;        /* host byte order */
    fRemoteAddr.sin_port = htons(inRemotePort); /* short, network byte order */
    fRemoteAddr.sin_addr.s_addr = inet_addr(inRemoteAddr);

    /* don't forget to error check the connect()! */
    int err = ::connect(m_s32FileDesc, (sockaddr *)&fRemoteAddr, sizeof(fRemoteAddr));
    m_u32State |= kConnected;

    if (err == -1)
    {
        fRemoteAddr.sin_port = 0;
        fRemoteAddr.sin_addr.s_addr = 0;
        return (OS_Error)OSThread::GetErrno();
    }

    return OS_NoErr;
}

OS_Error TCPSocket::SendTo(void* inBuffer, UInt32 inLength)
{
#ifdef __sgi__
	int theErr = ::send(m_s32FileDesc, inBuffer, inLength, 0);
#else
	// __Win32__ says that inBuffer is a char*
	int theErr = ::send(m_s32FileDesc, (char*)inBuffer, inLength, 0);
#endif

	if (theErr == -1)
		return (OS_Error)OSThread::GetErrno();
	return OS_NoErr;
}
OS_Error TCPSocket::RecvFrom(void* ioBuffer, UInt32 inBufLen, UInt32* outRecvLen)
{
	assert(outRecvLen != NULL);

#ifdef __sgi__
	SInt32 theRecvLen = ::recv(m_s32FileDesc, ioBuffer, inBufLen, 0);
#else
	// Win32 says that ioBuffer is a char*
	SInt32 theRecvLen = ::recv(m_s32FileDesc, (char*)ioBuffer, inBufLen, 0);
#endif

	if (theRecvLen == -1)
		return (OS_Error)OSThread::GetErrno();

	assert(theRecvLen >= 0);
	*outRecvLen = (UInt32)theRecvLen;
	return OS_NoErr;
}